package com.company;


import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Task3 {
    private static String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日",
            "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd"};


    public static void main(String[] args) {
        //todo 3.写个方法，字符串转Date，Date parse(String s)，调用方式，如：
        // Date t1 = parse("2006-01-02 03:04:05")
        // Date t2 = parse("2006-01-02")
        // Date t3 = parse("2006/01/02 03:04:05")
        // Date t4 = parse("2006/01/02")
        // 注1：不可增加日期格式参数，如：Date parse(String s, String pattern)，pattern 传 yyyy-MM-dd HH:mm:ss
        // 注2：可用三方工具类

        Date t1 = parse("2006-01-02 03:04:05");
        System.out.println(t1);
        Date t2 = parse("2006-01-02");
        System.out.println(t2);
        Date t3 = parse("2006/01/02 03:04:05");
        System.out.println(t3);
        Date t4 = parse("2006/01/02");
        System.out.println(t4);
    }


    public static Date parse(String s) {
        try {
            Date date = DateUtils.parseDate(s, parsePatterns);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatterStr = formatter.format(date);
            return formatter.parse(formatterStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
